<?php
	$MaHocSinh =  $_POST['MaHocSinh'];
	$HocKy = $_POST['HocKy'];
	$NamHoc = $_POST['NamHoc'];
	$MaMonHoc = $_POST['MaMonHoc'];
	$KiemTra_Mot =  $_POST['KiemTra_Mot'] ;
	$KiemTra_Hai = $_POST['KiemTra_Hai'];
	$KiemTra_Ba = $_POST['KiemTra_Ba'] ;
	$DTB_HK = round(DTBMH_HK($KiemTra_Mot,$KiemTra_Hai,$KiemTra_Ba),2);
	if(isset($_POST['ThiLai']))
	$ThiLai = $_POST['ThiLai'] ;
	$diemhk = new diemhkbs();
	
	$diemhk->MaHocSinh = $MaHocSinh;
	$diemhk->MaMonHoc = $MaMonHoc;
	$diemhk->HocKy = $HocKy;
	$diemhk->NamHoc = $NamHoc;
	
	$danhsach = $diemhk->GetList();
	if($danhsach)
	{
		$diemhk->MaHocSinh = $MaHocSinh;
		$diemhk->MaMonHoc = $MaMonHoc;
		$diemhk->HocKy = $HocKy;
		$diemhk->NamHoc = $NamHoc;
		$diemhk->KiemTra_Mot = $KiemTra_Mot;
		$diemhk->KiemTra_Hai = $KiemTra_Hai;
		$diemhk->KiemTra_Ba = $KiemTra_Ba;
		$diemhk->DTB_HK = $DTB_HK;
		
		if($DTB_HK < 3.5)
		{
			$diemhk->ThiLai = 1;
			$diemthilai = new diemthilaibs();
			$diemthilai->MaHocSinh = $MaHocSinh;
			$diemthilai->MaMonHoc = $MaMonHoc;
			$diemthilai->HocKy = $HocKy;
			$diemthilai->NamHoc = $NamHoc;
			$ds = $diemthilai->GetList();
			if($ds)
			{
				$diemthilai->DiemThi = $ds[0]['DiemThi'];
				$diemthilai->DiemTB = round(DTBMH_HK($KiemTra_Mot,$KiemTra_Hai,$ds[0]['DiemThi']),2);
				$diemthilai->Update();
			}else
			{
				$diemthilai->Add();
			}
			
		}
		$kq = $diemhk->Update();
		
		if($HocKy == "Học kỳ II")
		{
			
			$diemhk = new diemhkbs();
			
			$diemhk->MaHocSinh = $MaHocSinh;
			$diemhk->MaMonHoc = $MaMonHoc;
			$diemhk->NamHoc = $NamHoc;
			$dmdiemhk = $diemhk->GetList();
			$diemcn = new diemcnbs();
			$diemcn->MaHocSinh = $dmdiemhk[0]['MaHocSinh'];
			$diemcn->MaMonHoc = $dmdiemhk[0]['MaMonHoc'];
			$diemcn->NamHoc = $dmdiemhk[0]['NamHoc'];
			$dmdiemcn = $diemcn->GetList();
			$DTB_CN = DTBMH_CN($dmdiemhk[0]['DTB_HK'],$dmdiemhk[1]['DTB_HK']);
			$diemcn->DTB_CN = $DTB_CN;
			if($dmdiemcn)
			{
				$diemcn->Update();
			}else
			{
				$diemcn->Add();
			}
			
		}
		
		$DTB_HK = round(DTB_HK($MaHocSinh, $NamHoc, $HocKy),2);
		$xeploai = new xeploaihkbs();
		$xeploai->MaHocSinh = $MaHocSinh;
		$xeploai->HocKy = $HocKy;
		$xeploai->NamHoc = $NamHoc;
	
		$kt = $xeploai->GetList();
		
		$xeploai->MaHocSinh = $MaHocSinh;
		$xeploai->HocKy = $HocKy;
		$xeploai->NamHoc = $NamHoc;
		$xeploai->MaHanhKiem = $kt[0]['MaHanhKiem'];
		$xeploai->DiemTBHK = $DTB_HK;
		if($DTB_HK >= 8)
		{
			if(LoaiGioi($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Giỏi";
			else
				$xeploai->XepLoaiHK = "Khá";
		}elseif($DTB_HK >= 6.5 && $DTB_HK < 7.9)
		{
			if(LoaiKha($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Khá";
			else
				$xeploai->XepLoaiHK = "Trung bình";
		}elseif($DTB_HK >= 5 && $DTB_HK < 6.4)
		{
			if(LoaiTB($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Trung bình";
			else
				$xeploai->XepLoaiHK = "Yếu";
		}elseif($DTB_HK >= 3.5 && $DTB_HK < 4.9)
		{
			if(LoaiYeu($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Yếu";
			else
				$xeploai->XepLoaiHK = "Kém";
		}else
			$xeploai->XepLoaiHK = "Kém";
		if($kt)			
			$kq1=$xeploai->Update();
		else			
			$kq1=$xeploai->Add();
		if ($kq&&$kq1)
			header("Location: hocsinh-diemhk-". $MaHocSinh."");
		else
			ThongBaoLoi("Lỗi truy vấn!");
	}
	else
	{
		ThongBaoLoi("Mã không tồn tại!");
	}

?>